package asia.hymsk.spring.service.impl;

import asia.hymsk.spring.dao.*;
import asia.hymsk.spring.mapper.UserMapper;
import asia.hymsk.spring.pojo.Response;
import asia.hymsk.spring.pojo.mongodb.*;
import asia.hymsk.spring.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 花于陌上开
 * @date 2023-06-27 23:17
 */
@Service
public class RecommendServiceImpl implements RecommendService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RateMoreRecentlyProductsRepository rateMoreRecentlyProductsRepository;
    @Autowired
    RateMoreProductsRepository rateMoreProductsRepository;
    @Autowired
    AverageProductsRepository averageProductsRepository;
    @Autowired
    UserRecsRepository userRecsRepository;
    @Autowired
    StreamRecsRepository streamRecsRepository;

    @Override
    public Response historyList() {
        //查询列表
        List<RateMoreProducts> history=rateMoreProductsRepository.findAll();
        history=history.stream().sorted(Comparator.comparingInt(RateMoreProducts::getCount).reversed()).collect(Collectors.toList());
        return Response.success("请求成功",history);
    }

    @Override
    public Response averageList() {
        //查询列表
        List<AverageProducts> average=averageProductsRepository.findAll();
        average=average.stream().sorted(Comparator.comparingDouble(AverageProducts::getAvg).reversed()).collect(Collectors.toList());
        return Response.success("请求成功",average);
    }

    @Override
    public Response recentList(String dateTime) {
        //查询列表
        List<RateMoreRecentlyProducts> recent=rateMoreRecentlyProductsRepository.findAllByYearmonth(dateTime);
        recent=recent.stream().sorted(Comparator.comparingDouble(RateMoreRecentlyProducts::getCount).reversed()).collect(Collectors.toList());
        return Response.success("请求成功",recent);
    }

    @Override
    public Response recommend(String userName) {
        //查询用户推荐列表
        Integer userId = userMapper.selectByName(userName).getUserId();
        UserRecs recommend=userRecsRepository.findByUserId(userId);
        Stream<Recs> recommendStream= Arrays.stream(recommend.getRecs()).sorted(Comparator.comparingDouble(Recs::getScore).reversed());
        recommend.setRecs(recommendStream.toArray(Recs[]::new));
        return Response.success("请求成功",recommend);
    }

    @Override
    public Response realtime(String userName) {
        //查询用户推荐列表
        Integer userId = userMapper.selectByName(userName).getUserId();
        StreamRecs recommend=streamRecsRepository.findByUserId(userId);
        Stream<Recs> recommendStream= Arrays.stream(recommend.getRecs()).sorted(Comparator.comparingDouble(Recs::getScore).reversed());
        recommend.setRecs(recommendStream.toArray(Recs[]::new));
        return Response.success("请求成功",recommend);
    }
}
